package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Menu;
import cn.itcast.service.SecutityUserService;

import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class SecurityUserController {

    @Reference
    private SecutityUserService secutityUserService;

    @GetMapping("/getUsername")
    public Result getUsername(){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        try {
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

//    @GetMapping("/menuList")
//    public Result menuList(){
//
//        try {
//            Menu menuList = secutityUserService.getMenuList();
//            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,menuList);
//        } catch (RuntimeException e) {
//            return new Result(false, e.getMessage());
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
//        }
//    }
}
